package 富婆梦.第一阶段.第二周.作业;

import java.util.LinkedList;
import java.util.List;

public class test {
    public static void main(String[] args) {
        //一把手枪，两个玩家
        Gun gun = new Gun();
        Player p1 = new Player("小A");
        Player p2 = new Player("小B");
        Player p3 = new Player("小C");
        Player p4 = new Player("小D");
        List<Player> players = new LinkedList();
        players.add(p1);
        players.add(p2);
        players.add(p3);
        players.add(p4);
        List list = new LinkedList();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);

        //子弹上膛，转动转轮
        gun.putBullet(1);
        gun.turn();
        System.out.println("游戏开始前：");
        for (Player p:players) {
            System.out.println(p.getName()+"是否活着"+p1.isLife());
        }
        System.out.println("--------游戏正式开始--------");
        new test().startShoot(list,gun);
        System.out.println("游戏结束后：");
        for (Player p:players) {
            System.out.println(p.getName()+"是否活着"+p1.isLife());
        }

    }

    //多人游戏，按顺序轮流开枪
    public void startShoot(List<Player> list,Gun gun){
        int count = 0;
        outer:
        while (true) {
            for (int i = 0; i < list.size(); i++) {
                //活着的人才可以开枪
                if (list.get(i).isLife() == true) {
                    //这一枪有子弹
                    if (list.get(i).fire(gun) == true) {
                        //开枪者为数组里的最后一个
                        if (i == list.size() - 1) {
                            //数组第一个人死了
                            list.get(0).gotShoot();
                            System.out.print("这是第" + count + "枪,成功击中" + list.get(0).getName());
                            System.out.println("。还剩下" + gun.getBullet() + "发子弹，开枪者为" + list.get(i).getName());
                            list.remove(0);
                        } else {
                            //后面一个人死掉
                            list.get(i + 1).gotShoot();
                            System.out.print("这是第" + count + "枪,成功击中" + list.get(i + 1).getName());
                            System.out.println("。还剩下" + gun.getBullet() + "发子弹，开枪者为" + list.get(i).getName());
                            list.remove(i + 1);
                        }
                    } else {//枪里没有子弹
                        System.out.print("这是第" + count + "枪,没有击中对方。还剩下");
                        System.out.println(gun.getBullet() + "发子弹，开枪者为" + list.get(i).getName());
                    }
                    count = count + 1;
                }
                if (gun.getBullet()==0){
                    System.out.println("子弹已经用光了！");
                    System.out.println("--------游戏结束--------");
                    break outer;
                }

            }
        }

    }
}
